import queue
import threading
import time

# 初始化一个容器来装包子
q = queue.Queue()
# 最大生产量
max_count = 10


# 生产者
def produce(name):
    # 包子个数
    count = 0
    while count < max_count:
        print("%s在包子中..." % name)
        time.sleep(2)
        print("%s已经做好了第%s个包子" % (name, count + 1))
        count += 1
        # 容器中的包子
        q.put(count)
        # 等待接收信息
        q.join()
        print("\nOK...")


# 消费组
def consumer(name):
    # 吃掉的包子
    count = 0
    while count < max_count:
        time.sleep(2)
        # 取包子,吃包子
        data = q.get()
        print("%s eating..." % name)
        # 吃包子用了4s然后给厨师发送一个信号
        time.sleep(4)
        q.task_done()
        count += 1
        print('\033[32;1mConsumer %s已经把第%s个包子吃了...\033[0m' % (name, data))


if __name__ == '__main__':
    p1 = threading.Thread(target=produce, args=('厨师',))
    s1 = threading.Thread(target=consumer, args=("学学",))
    s2 = threading.Thread(target=consumer, args=("学哥",))
    s3 = threading.Thread(target=consumer, args=("学帅啊",))

    p1.start()
    s1.start()
    s2.start()
    s3.start()
